/**
 * 
 */
package co.com.smart.eagle.client;

import co.com.smart.eagle.client.factory.EagleFactory;
import co.com.smart.eagle.client.mvp.AppActivityMapper;
import co.com.smart.eagle.client.mvp.AppPlaceHistoryMapper;
import co.com.smart.eagle.client.place.CrearAplicacionesPlace;
import co.com.smart.eagle.client.place.CrearRecursosPlace;
import co.com.smart.eagle.client.place.CrearRolesPlace;
import co.com.smart.eagle.client.place.CrearUsuariosPlace;
import co.com.smart.eagle.client.place.HomePlace;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.web.bindery.event.shared.EventBus;

/**
 * @author afescobar
 *
 */
public class SmartEagleEntryPoint implements EntryPoint {
	
	private Place defaultPlace = new HomePlace();
	private SimplePanel appWidget = new SimplePanel();
	private PlaceController placeController;
	private AppPlaceHistoryMapper historyMapper;
	
	private interface CallBackInit{
		void end();
	}

	/**
	 * 
	 */
	public SmartEagleEntryPoint() {
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
	 */
	@Override
	public void onModuleLoad() {
		createMenu(new CallBackInit() {
			@Override
			public void end() {
				initGWTModule();
			}
		});
	}
	
	private void createMenu(CallBackInit callBack){
		MenuBar menuBar = new MenuBar();
		MenuBar submenu = new MenuBar(true);
		MenuBar submenuRecursos = new MenuBar(true);
		RootPanel.get("menu").add(menuBar);
		/**
		 * Creacion de maestros
		 */
		submenu.addItem("Usuario", new Command() {

			@Override
			public void execute() {
					placeController.goTo(new CrearUsuariosPlace());
			}
		});
		submenu.addItem("Roles", new Command() {

			@Override
			public void execute() {
//				Place palce = historyMapper.getPlace("CrearRolesPlace:".replace("/SmartEagle.html#", ""));
					placeController.goTo(new CrearRolesPlace());
			}
		});
		submenu.addItem("Aplicaciones", new Command() {

			@Override
			public void execute() {
					placeController.goTo(new CrearAplicacionesPlace());
			}
		});
		menuBar.addItem("Crear", submenu);
		
		
		/**
		 * Creacion de recuros y permisos
		 */
		
	    submenuRecursos.addItem("Crear Recursos", new Command() {

			@Override
			public void execute() {
					placeController.goTo(new CrearRecursosPlace());
			}
		});
	    submenuRecursos.addItem("Crear men\u00FA", new Command() {

			@Override
			public void execute() {
				Place palce = historyMapper.getPlace("/CrearUsuarioPlace:".replace("/SmartEagle.html#", ""));
					placeController.goTo(palce);
			}
		});
		menuBar.addItem("Administrar recursos", submenuRecursos);
		callBack.end();
	}
	
	private void initGWTModule() {
		EagleFactory eagleFactory = GWT.create(EagleFactory.class);
		EventBus eventBus = eagleFactory.getEventBus();
		placeController = eagleFactory.getPlaceController();

		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(eagleFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper,eventBus);
		activityManager.setDisplay(appWidget);

		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		historyMapper = GWT.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);
		RootPanel.get("contenido").add(appWidget);

//		Label version = new Label(Utilidades.cons.version());
//		version.setStylePrimaryName("version");
//		RootPanel.get("version").add(version);

		// Goes to place represented on URL or default place

		historyHandler.handleCurrentHistory();
	}
	

}
